package com.pan.basecommon.model.http.control;

import android.content.Context;

import com.pan.basecommon.ui.loader.LoadingDialog;

import io.reactivex.disposables.Disposable;

public abstract class ProgressObserver<T> extends BaseObserver<T> {

    private LoadingDialog progressDialog;
    private Context mContext;
    private String mLoadingText;

    public ProgressObserver(Context context){
        this(context,null);
    }

    public ProgressObserver(Context context,String loadingText){
        this.mContext=context;
        this.mLoadingText=loadingText;
    }

    @Override
    public void onSubscribe(Disposable d) {
        if(!d.isDisposed()){
            progressDialog = new LoadingDialog(mContext);
            progressDialog.setLoadingText(mLoadingText == null ? "正在加载中..." : mLoadingText);
            progressDialog.show();
        }
    }

    @Override
    public void onComplete() {
        super.onComplete();
        if (progressDialog != null) {
            progressDialog.close();
        }

    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        if (progressDialog != null) {
            progressDialog.close();
        }
    }
}
